﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Drawing;

namespace Rule
{
    public class ImageSizeTemplateRule : TemplateRule
    {
        private float minWidth;

        public float MinWidth
        {
            get { return minWidth; }
            set { minWidth = value; }
        }

        private float maxWidth;

        public float MaxWidth
        {
            get { return maxWidth; }
            set { maxWidth = value; }
        }

        private float minHeight;

        public float MinHeight
        {
            get { return minHeight; }
            set { minHeight = value; }
        }

        private float maxHeight;

        public float MaxHeight
        {
            get { return maxHeight; }
            set { maxHeight = value; }
        }

        private int dpi = 150;

        public int Dpi
        {
            get { return dpi; }
            set { dpi = value; }
        }

        public override bool parseFromXmlNode(XmlNode node)
        {
            if (node == null)
                return false;

            XmlAttribute minWidthAttr = node.Attributes["minWidth"];
            XmlAttribute maxWidthAttr = node.Attributes["maxWidth"];
            XmlAttribute minHeightAttr = node.Attributes["minHeight"];
            XmlAttribute maxHeightAttr = node.Attributes["maxHeight"];

            if (minWidthAttr == null && maxWidthAttr == null)
            {
                Debug.WriteLine("最大与最小宽度均不存在。");
                return false;
            }

            if (minHeightAttr == null && maxHeightAttr == null)
            {
                Debug.WriteLine("最大与最小高度均不存在。");
                return false;
            }
            
            if (minWidthAttr == null || !float.TryParse(minWidthAttr.Value, out minWidth))
            {
                Debug.WriteLine("最小宽度值不正确。");
                return false;
            }

            if (maxWidthAttr == null || !float.TryParse(maxWidthAttr.Value, out maxWidth))
            {
                Debug.WriteLine("最大宽度值不正确。");
                return false;
            }

            if (minHeightAttr == null || !float.TryParse(minHeightAttr.Value, out minHeight))
            {
                Debug.WriteLine("最小高度值不正确。");
                return false;
            }

            if (maxHeightAttr == null || !float.TryParse(maxHeightAttr.Value, out maxHeight))
            {
                Debug.WriteLine("最大高度值不正确。");
                return false;
            }

            return true;
        }

        public override string ToString()
        {
            return string.Format("MinWidth: {0}, MaxWidth: {1}, MinHeight: {2}, MaxHeight: {3}",
                minWidth, maxWidth, minHeight, maxHeight);
        }

        public override bool match(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                return false;

            Bitmap img = new Bitmap(fileName);
            if (img.Width <= TemplateRuleHalper.mm2Pixel(minWidth, dpi) || 
                img.Width >= TemplateRuleHalper.mm2Pixel(maxWidth, dpi) || 
                img.Height <= TemplateRuleHalper.mm2Pixel(minHeight, dpi) || 
                img.Height >= TemplateRuleHalper.mm2Pixel(maxHeight, dpi))
            {
                return false;
            }

            return true;
        }
    }
}
